<?php
namespace app\cdyoa\controller;
use think\Db;
class Orders extends Base
{   
    public function lists(){
        if( is_post() ){
            $page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            $sql = "
                SELECT
                    [1]
                FROM
                    orders
                WHERE 
                    1 = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and (order_no like ? or title like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);
            
            //列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by id desc limit $start, $page_size";
            $field       = "*";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']      =  com_format_checkbox($v['id']);
                $list[$k]['create_time']   =  com_format_time( $v['create_time'] );
                $list[$k]['status']        =  c('type.orders_status',$v['status']);
                $list[$k]['is_zd']         =  c('type.is_zd',$v['is_zd']);
                
                $list[$k]['content']       =  com_substr(strip_tags($v['content']), 12);
                if(com_check_type($v['attach'])){
                    $list[$k]['attach']        =  com_format_pic(attach($v['attach']));
                }else{
                    $list[$k]['attach']        =  com_format_a(attach($v['attach']));
                }
                
                //操作按钮
                $list[$k]['action']        = com_page(['ico'=>'yanjing', 'title'=>'查看订单', 'url'=>url('orders/detail','id='.$v['id'])]);
                if($v['status']==0||$v['status']==3){
                    $list[$k]['action']       .= com_page(['ico'=>'edit', 'title'=>'修改订单', 'url'=>url('orders/edit','id='.$v['id'])]);
                    $list[$k]['action']       .= com_page(['ico'=>'del3','title'=>'删除','url'=>url('c/del'), 'post'=>['id'=>$v['id'],'t'=>'orders'],'confirm'=>"你确定要删除[{$v['title']}]吗"]);
                }
                //$list[$k]['action']       = com_page(['ico'=>'edit', 'title'=>'修改订单', 'url'=>url('orders/edit','id='.$v['id'])]);
                //$list[$k]['action']       .= com_page(['ico'=>'manage', 'title'=>'批注订单', 'url'=>url('orders/manage','id='.$v['id']),'width'=>600, 'height'=>400]);
                //$list[$k]['action']       .= com_page(['ico'=>'del3','title'=>'删除','url'=>url('orders/delete'), 'post'=>['id'=>$v['id']],'confirm'=>"你确定要删除[{$v['title']}]吗"]);
            }
            $com_body    = com_build_table($list, array("id","order_no", "title", "contract_no","content","sale_price","fob","haiyun","is_zd", "status", "create_time","action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data,'suc'=>['js_action'=>'function.iphone']]);
        }
        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.orders_status'),
            'nav'       => ['计划订单列表', '列表'],
        ];
        return view('', $data);
    }  
    public function search(){
        if( is_post() ){
            $page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            $sql = "
                SELECT
                    [1]
                FROM
                    orders
                WHERE 
                    status = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and (order_no like ? or title like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);
            
            //列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by id desc limit $start, $page_size";
            $field       = "*";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']      =  com_format_checkbox($v['id']);
                $list[$k]['create_time']   =  com_format_time( $v['create_time'] );
                //操作按钮
                $list[$k]['action']       = com_page(['txt'=>'收款', 'title'=>'选择订单收款项', 'url'=>url('inshou/bangdin','id='.$v['id'])]);
                $list[$k]['action']       .= com_page(['txt'=>'退税', 'title'=>'退税', 'url'=>url('intui/add','id='.$v['id']),'width'=>600,'height'=>600]);
                $list[$k]['action']       .= com_page(['txt'=>'直材', 'title'=>'直材', 'url'=>url('outzc/add','id='.$v['id']),'width'=>600,'height'=>600]);
                $list[$k]['action']       .= com_page(['txt'=>'报销', 'title'=>'报销', 'url'=>url('outbx/add','id='.$v['id']),'width'=>800,'height'=>800]);
            }
            $com_body    = com_build_table($list, array("id","order_no", "title","contract_no", "create_time","action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data]);
        }
        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.orders_status'),
            'nav'       => ['计划订单列表', '列表'],
        ];
        return view('', $data);
    }

    public function add(){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                'order_no'      =>  'require',
                'title'        =>  'require',
                'us_sale_price'      =>  'require|float',
                'nums'             =>  'require|float',
            ];
            $msg = [
                'title.require'       =>  '标题不能为空',
                'us_sale_price.require'       =>  '请填写美元售价',
            ];
            com_validate( $rule, $msg, $data, true );
            $data['create_time'] = time();
            $data['sale_price'] = $data['sale_price']?$data['sale_price']:0;
            $data['fob'] = $data['fob']?$data['fob']:0;
            $data['haiyun'] = $data['haiyun']?$data['haiyun']:0;
            Db::name('orders')->insert($data);
            $config = [
                'msg' => '添加计划订单成功！',
                'suc' => [
                    'url' => url('publics/suc'),
                    'js_action' => 'alert',
                ],
            ];
            com_check( $config );
        }    
        $data = [
            'nav'       => ['订单录入', '表单'],
        ];
        return view('', $data);
    }


    //编辑
    public function edit( $id='' ){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                'order_no'      =>  'require',
                'title'        =>  'require',
                'us_sale_price'      =>  'require|float',
                'nums'             =>  'require|float',
            ];
            $msg = [
                'title.require'       =>  '标题不能为空',
                'us_sale_price.require'       =>  '请填写美元售价',
            ];
            com_validate( $rule, $msg, $data, true );
            $data['update_time'] = time();
            $data['id']         = $id;
            $data['sale_price'] = $data['sale_price']?$data['sale_price']:0;
            $data['fob'] = $data['fob']?$data['fob']:0;
            $data['haiyun'] = $data['haiyun']?$data['haiyun']:0;
            Db::name('orders')->update($data);
            $config = [
                'msg' => '修改计划订单成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $data = [
            'info' => Db::name('orders')->find($id),
            'id'  => $id,
        ];
        return view('', $data);
    }
    
    //详情
    public function detail($id=''){
        #列表
        $sql = "select a.*, b.name as lname from in_shou a left join admin_user b on a.user_id = b.id where a.orders_id = ? and a.status in (1,2) order by a.id desc";
        $shou = Db::query($sql, [$id]);
        
        $sql = "select a.*, b.name as lname from in_tui a left join admin_user b on a.user_id = b.id where a.orders_id = ? and a.status in (0,1) order by a.id desc";
        $tui = Db::query($sql, [$id]);
        
        $sql = "select a.*, b.name as lname from out_zc a left join admin_user b on a.user_id = b.id where a.orders_id = ? and a.status in (0,1) order by a.id desc";
        $zc = Db::query($sql, [$id]);
        
        $sql = "select a.*, b.name as lname from out_bx a left join admin_user b on a.user_id = b.id where a.orders_id = ? and a.one_user_status=1 and a.two_user_status=1 order by a.id desc";
        $bx = Db::query($sql, [$id]);
        
        #财务
        $zshou = Db::name("in_shou")->where('orders_id',$id)->where('status',2)->value("sum(rmb)");
        $ztui = Db::name("in_tui")->where('orders_id',$id)->where('status',1)->value("sum(rmb)");
        $zzc = Db::name("out_zc")->where('orders_id',$id)->where('status',1)->value("sum(rmb)");
        $zbx = Db::name("out_bx")->where('orders_id',$id)->where('one_user_status',1)->where('two_user_status',1)->value("sum(money)");
        $zlr = $zshou+$ztui-$zzc-$zbx;
        
        $data = [
            'info' => Db::name('orders')->find($id),
            'shou' => $shou,
            'tui'  => $tui,
            'zc'   => $zc,
            'bx'   => $bx,
            'zlr' => $zlr,
            'zshou' => $zshou,
            'ztui'  => $ztui,
            'zzc'   => $zzc,
            'zbx'   => $zbx,
        ];
        return view('', $data);
    }
}
